package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.domain.ErpWareAllocate;
import com.cleaningcloud.erp.domain.dto.ErpWareAllocateDto;
import com.cleaningcloud.erp.domain.excelExport.WareAllocateExcelExport;
import com.cleaningcloud.erp.domain.vo.ErpWareAllocateDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpWareAllocateVo;
import com.cleaningcloud.erp.service.IErpWareAllocateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 调拨Controller
 *
 * @author cleaningcloud
 * @date 2024-10-08
 */
@RestController
@RequestMapping("/ErpWareAllocate")
public class ErpWareAllocateController extends BaseController {
    @Autowired
    private IErpWareAllocateService erpWareAllocateService;

    /**
     * 查询调拨列表
     */
    @RequiresPermissions("erp:ErpWareAllocate:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpWareAllocate erpWareAllocate) {
        startPage();
        List<ErpWareAllocateVo> list = erpWareAllocateService.selectErpWareAllocateList(erpWareAllocate);
        return getDataTable(list);
    }

    /**
     * 导出调拨列表
     */
    @RequiresPermissions("erp:ErpWareAllocate:export")
    @Log(title = "调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpWareAllocate erpWareAllocate) {
        List<ErpWareAllocateVo> list = erpWareAllocateService.selectErpWareAllocateList(erpWareAllocate);
        ExcelUtil<ErpWareAllocateVo> util = new ExcelUtil<ErpWareAllocateVo>(ErpWareAllocateVo.class);
        util.exportExcel(response, list, "调拨数据");
    }

    /**
     * 导出调拨明细列表
     */
    @RequiresPermissions("erp:ErpWareAllocate:export")
    @Log(title = "调拨", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{wareAllocateId}")
    public void exportDetails(HttpServletResponse response, @PathVariable Long wareAllocateId) {
        ErpWareAllocateVo wareAllocateVo = erpWareAllocateService.selectErpWareAllocateByWareAllocateId(wareAllocateId);
        List<WareAllocateExcelExport> list = new ArrayList<>();
        for (ErpWareAllocateDetailsVo erpWareAllocateDetailsVo : wareAllocateVo.getDetailsList()) {
            WareAllocateExcelExport wareAllocateExcelExport = new WareAllocateExcelExport();
            wareAllocateExcelExport.setWareAllocateOdd(wareAllocateVo.getWareAllocateOdd());
            wareAllocateExcelExport.setWareOutName(wareAllocateVo.getWareOutName());
            wareAllocateExcelExport.setOutPersonName(wareAllocateVo.getOutPersonName());
            wareAllocateExcelExport.setWareInName(wareAllocateVo.getWareInName());
            wareAllocateExcelExport.setInPersonName(wareAllocateVo.getInPersonName());
            wareAllocateExcelExport.setWareAllocateDate(wareAllocateVo.getWareAllocateDate());
            wareAllocateExcelExport.setCreateName(wareAllocateVo.getCreateName());
            wareAllocateExcelExport.setApproveName(wareAllocateVo.getApproveName());
            wareAllocateExcelExport.setApproveTime(wareAllocateVo.getApproveTime());
            wareAllocateExcelExport.setCommodityNo(erpWareAllocateDetailsVo.getCommodityNo());
            wareAllocateExcelExport.setCommodityName1(erpWareAllocateDetailsVo.getCommodityName1());
            wareAllocateExcelExport.setCommodityType(erpWareAllocateDetailsVo.getCommodityType());
            wareAllocateExcelExport.setCommoditySpec(erpWareAllocateDetailsVo.getCommoditySpec());
            wareAllocateExcelExport.setUnitName1(erpWareAllocateDetailsVo.getUnitName1());
            wareAllocateExcelExport.setUnitRelationship(erpWareAllocateDetailsVo.getUnitRelationship());
            wareAllocateExcelExport.setWareAllocateNumber(erpWareAllocateDetailsVo.getWareAllocateNumber());
            list.add(wareAllocateExcelExport);
        }
        ExcelUtil<WareAllocateExcelExport> util = new ExcelUtil<WareAllocateExcelExport>(WareAllocateExcelExport.class);
        util.exportExcel(response, list, "库存调拨");
    }

    /**
     * 获取调拨详细信息
     */
    @RequiresPermissions("erp:ErpWareAllocate:query")
    @GetMapping(value = "/{wareAllocateId}")
    public AjaxResult getInfo(@PathVariable("wareAllocateId") Long wareAllocateId) {
        return success(erpWareAllocateService.selectErpWareAllocateByWareAllocateId(wareAllocateId));
    }

    /**
     * 新增调拨
     */
    @RequiresPermissions("erp:ErpWareAllocate:add")
    @Log(title = "调拨", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpWareAllocateDto erpWareAllocate) {
        return toAjax(erpWareAllocateService.insertErpWareAllocate(erpWareAllocate));
    }

    /**
     * 修改调拨
     */
    @RequiresPermissions("erp:ErpWareAllocate:edit")
    @Log(title = "调拨", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpWareAllocateDto erpWareAllocate) {
        return toAjax(erpWareAllocateService.updateErpWareAllocate(erpWareAllocate));
    }


    /**
     * 审核调拨
     */
    @RequiresPermissions("erp:ErpWareAllocate:audit")
    @Log(title = "审核调拨", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody ErpWareAllocate erpWareAllocate) {
        return toAjax(erpWareAllocateService.auditErpWareAllocate(erpWareAllocate));
    }

    /**
     * 删除调拨
     */
    @RequiresPermissions("erp:ErpWareAllocate:remove")
    @Log(title = "调拨", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wareAllocateIds}")
    public AjaxResult remove(@PathVariable Long[] wareAllocateIds) {
        return toAjax(erpWareAllocateService.deleteErpWareAllocateByWareAllocateIds(wareAllocateIds));
    }
}
